package indzcraft.loader;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.HashMap;


public class Equipment {
	
	public HashMap<Slot,ItemEquipped> equipment = new HashMap<Slot,ItemEquipped>();
	
	private PropertyChangeSupport changes = new PropertyChangeSupport( this );
	
	public Equipment(){
		//
	}
	
	public ItemEquipped get(Slot slot){
		return equipment.get(slot);
	}
	
	public void put(Slot slot, ItemEquipped item) {
		ItemEquipped old = equipment.get(slot);
		equipment.put(slot, item);
		changes.firePropertyChange( slot.getName(), old, item );		
	}
	
	public void put(Slot slot,Item item){
		ItemEquipped _new = new ItemEquipped();
		_new.setItem(item);
		ItemEquipped old = equipment.get(slot);
		equipment.put(slot, _new);
		changes.firePropertyChange( slot.getName(), old, _new );
	}
	
	public void setEnchant(Slot slot,Enchant e){
		Enchant old = equipment.get(slot).getEnchant();
		equipment.get(slot).setEnchant(e);
		changes.firePropertyChange( slot.getName()+"_enchant", old, e);
	}
	
	public void setGem(Slot slot, int gemSlot, Gem gem) {
		Gem old = equipment.get(slot).getGem(gemSlot);
		equipment.get(slot).setGem(gemSlot, gem);
		changes.firePropertyChange( slot.getName()+"_"+gemSlot+"_gem", old, gem);
	}
	
	public void setReforge(Slot slot, Reforge r) {
		Reforge old = equipment.get(slot).getReforge();
		equipment.get(slot).setReforge(r);
		changes.firePropertyChange( slot.getName()+"_reforge", old, r);
	}
	
	public void setAutoReforge(Slot slot, Reforge r){
		equipment.get(slot).setReforge(r);
	}
	
	public void fireAutoReforgeComplete() {
		changes.firePropertyChange("autoreforge", false, true);
	}
	
	public void fireAutoGemComplete() {
		changes.firePropertyChange("autogem", false, true);
	}
	
	public void addExtraSocket(Slot slot) {
		for(int i = 0;i < equipment.get(slot).getItem().gemSlot.length; i++){
			if(equipment.get(slot).getItem().gemSlot[i].equals("PRISMATIC"))
				break;
			else if(equipment.get(slot).getItem().gemSlot[i].equals("")){
				equipment.get(slot).getItem().gemSlot[i] = "PRISMATIC";
				break;
			}
		}
	}
	
	public void removeExtraSocket(Slot slot) {
		for(int i = 0;i < equipment.get(slot).getItem().gemSlot.length; i++){
			if(equipment.get(slot).getItem().gemSlot[i].equals("PRISMATIC")){
				equipment.get(slot).getItem().gemSlot[i] = "";
				equipment.get(slot).setGem(i, null);
			}
		}
	}
	
	public void fireProfessionChangeComplete() {
		changes.firePropertyChange("professionchange", false, true);
	}
	
	public void print(){
		for(Slot slot: equipment.keySet()){
			System.out.println(slot.getName());
			equipment.get(slot).getItem().print();
			System.out.println("====");
		}
	}
	
	public Item getStats(){
		Item i = new Item(); // missbraucht als datenspeicher
		for(Slot slot: equipment.keySet()){
			ItemEquipped ie = equipment.get(slot);
			i.setHaste(i.getHaste()+ie.getReforgedHaste());
			i.setCrit(i.getCrit()+ie.getReforgedCrit());
			i.setHit(i.getHit()+ie.getReforgedHit());
			i.setMastery(i.getMastery()+ie.getReforgedMastery());
			i.setExp(i.getExp()+ie.getReforgedExp());
			// gems
			boolean colorMatch = true;
			for(int g = 0;g < ie.gem.length;g++){
				if(ie.getItem().gemSlot[g].equals("") || ie.gem[g] == null){
					colorMatch = false;
				}else{
					i.setHaste(i.getHaste()+ ie.gem[g].getHaste());
					i.setHit(i.getHit()+ ie.gem[g].getHit());
					i.setMastery(i.getMastery()+ ie.gem[g].getMastery());
				}
			}
			if(ie.getItem().socketBonus != null && colorMatch){
				i.setHaste(i.getHaste()+ie.getItem().socketBonus.getHaste());
				i.setMastery(i.getMastery()+ie.getItem().socketBonus.getMastery());
				i.setExp(i.getExp()+ie.getItem().socketBonus.getExp());
				i.setCrit(i.getCrit()+ie.getItem().socketBonus.getCrit());
				i.setHit(i.getHit()+ie.getItem().socketBonus.getHit());
			}
			// enchant
			if(ie.getEnchant() != null){
				Enchant e = ie.getEnchant();
				i.setHaste(i.getHaste()+e.getHaste());
				i.setMastery(i.getMastery()+e.getMastery());
				i.setExp(i.getExp()+e.getExp());
				i.setCrit(i.getCrit()+e.getCrit());
				i.setHit(i.getHit()+e.getHit());
			}
		}
		return i;
	}

	public Item getReforgings() {
		Item i = new Item(); // missbraucht als datenspeicher
		for(Slot slot: equipment.keySet()){
			Reforge r = equipment.get(slot).getReforge();
			if(r != null){
				i.setHaste(i.getHaste() + equipment.get(slot).getReforgedHaste()-equipment.get(slot).getItem().getHaste());
				i.setCrit(i.getCrit() + equipment.get(slot).getReforgedCrit()-equipment.get(slot).getItem().getCrit());
				i.setHit(i.getHit() + equipment.get(slot).getReforgedHit()-equipment.get(slot).getItem().getHit());
				i.setMastery(i.getMastery() + equipment.get(slot).getReforgedMastery()-equipment.get(slot).getItem().getMastery());
				i.setExp(i.getExp() + equipment.get(slot).getReforgedExp()-equipment.get(slot).getItem().getExp());
			}
		}
		return i;
	}
	
	public void addPropertyChangeListener(PropertyChangeListener l)
	{
		changes.addPropertyChangeListener(l);
	}

	public void removePropertyChangeListener(PropertyChangeListener l)
	{
		changes.removePropertyChangeListener(l);
	}
}
